package com.webserver.core;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.controller.ArticleController;
import com.webserver.controller.ToolsController;
import com.webserver.controller.UserController;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class DispatcherServlet {
    public static File root;
    public static File staticDir;
    static {//静态块不能抛异常
        try {
            root=new File(
                    DispatcherServlet.class.getClassLoader().getResource(".").toURI()
            );
            staticDir=new File(root,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public void service(HttpServletRequest request, HttpServletResponse response){
        String path=request.getRequestURI();
        System.out.println("============================>"+path);
        /*if ("/myweb/reg".equals(path)){
            UserController controller=new UserController();
            controller.reg(request, response);
            return;
        }else if("/myweb/login".equals(path)){
            UserController controller=new UserController();
            controller.login(request, response);
        }else if("/myweb/writeArticle".equals(path)){
            ArticleController controller=new ArticleController();
            controller.writeArticle(request, response);
        }else if("/myweb/showAllUser".equals(path)){
            UserController controller=new UserController();
            controller.showAllUser(request, response);
        }else if("/myweb/deleteUser".equals(path)){
            UserController controller=new UserController();
            controller.deleteUser(request, response);
        }else if ("/myweb/showAllArticle".equals(path)){
            ArticleController controller=new ArticleController();
            controller.showAllArticle(request, response);
        }else if ("/myweb/show".equals(path)){
            ArticleController controller=new ArticleController();
            controller.show(request, response);
        }else if ("/myweb/createQr".equals(path)){
            ToolsController controller=new ToolsController();
            controller.creatQr(request, response);
        }
        else {*/
        try {
            /*File dir=new File(
                    DispatcherServlet.class.getClassLoader().getResource("./com/webserver/controller").toURI()
            );
            File[] files=dir.listFiles(f->f.getName().endsWith(".class"));
            for (File file:files){
                Class cls=Class.forName("com.webserver.controller."+file.getName().substring(0,file.getName().lastIndexOf(".")));
                if (cls.isAnnotationPresent(Controller.class)){
                    Object obj=cls.newInstance();
                    Method[] methods=cls.getDeclaredMethods();
                    for (Method method:methods){
                        if (method.isAnnotationPresent(RequestMapping.class)){
                            RequestMapping rem=method.getAnnotation(RequestMapping.class);
                            String value=rem.value();
                            if (path.equals(value)){
                                method.invoke(obj,request,response);
                                return;
                            }
                        }
                    }
                }
            }*/
            HandlerMapping.MethodMapping methodMapping=HandlerMapping.getMethod(path);
            if (methodMapping!=null){
                methodMapping.getMethod().invoke(methodMapping.getController(),request,response);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        File file=new File(staticDir,path);
        System.out.println("资源是否存在: "+file.exists());
        if (file.isFile()){
            response.setContentFile(file);

        }else {
            file=new File(staticDir,"root/404.html");
            response.setStatusCode(404);
            response.setStatusReason("NotFound");
            response.setContentFile(file);

        }
        //}

        response.addHeader("Server","MyWebServer");
    }

}
